<template>
  <a-drawer
    :closable="false"
    :maskClosable="false"
    title="修改菜单"
    width="40%"
    :visible="visible"
    :body-style="{ paddingBottom: '80px'}"
    @close="onClose"
  >
    <a-tabs v-model="tabkey">
      <a-tab-pane key="1" tab="中文"></a-tab-pane>
      <a-tab-pane key="2" tab="英文" :forceRender="true"></a-tab-pane>
    </a-tabs>
    <a-form-model 
      ref="ruleForm"
      :model="form" 
      :rules="rules"
      :label-col="labelCol" 
      :wrapper-col="wrapperCol" 
      :key="visible"
    >
      <a-form-model-item label="特点所属" prop="menuId">
        <a-tree-select
          v-model="form.menuId"
          style="width: 100%; margin-bottom: 30px;"
          :dropdown-style="{ maxHeight: '400px', overflow: 'auto' }"
          :tree-data="parent.menuData"
          placeholder="特点所属"
          tree-default-expand-all
          allowClear
          :replaceFields="{
            title: 'title',
            key: 'id',
            value: 'id'
          }"
        >
        </a-tree-select>
      </a-form-model-item>
      <a-form-model-item label="特点标题" prop="title" v-if="tabkey == '1'">
        <a-input v-model="form.title" placeholder="特点标题" />
      </a-form-model-item>
      <a-form-model-item label="英文特点标题" prop="titleEn" v-else>
        <a-input v-model="form.titleEn" placeholder="英文特点标题" />
      </a-form-model-item>
      <a-form-model-item label="特点描述" prop="description" v-if="tabkey == '1'">
        <a-textarea
          v-model="form.description"
          placeholder="特点描述"
          :auto-size="{ minRows: 2, maxRows: 6 }"
        />
      </a-form-model-item>
      <a-form-model-item label="英文特点描述" prop="description" v-else>
        <a-textarea
          v-model="form.descriptionEn"
          placeholder="英文特点描述"
          :auto-size="{ minRows: 2, maxRows: 6 }"
        />
      </a-form-model-item>
      <a-form-model-item label="图片" required>
        <a-upload
          :customRequest="handleUploadChange"
          list-type="picture-card"
          :file-list="fileList"
          @preview="handlePreview"
          :remove="hanldeImgRemove"
        >
          <div v-if="fileList.length < 1">
            <a-icon type="plus" />
            <div class="ant-upload-text">上传图片(278*256)</div>
          </div>
        </a-upload>
      </a-form-model-item>
      <a-form-model-item label="排序" prop="orderNum">
        <a-input-number v-model="form.orderNum" placeholder="排序" style="width:200px" />
      </a-form-model-item>
    </a-form-model>
    <div
      :style="{
        position: 'absolute',
        right: 0,
        bottom: 0,
        width: '100%',
        borderTop: '1px solid #e9e9e9',
        padding: '10px 16px',
        background: '#fff',
        textAlign: 'right',
        zIndex: 99999,
      }"
    >
      <a-button :style="{ marginRight: '8px' }" @click="onClose"> 关闭 </a-button>
      <a-button type="primary" @click="onSubmit"> 提交 </a-button>
    </div>
    <a-modal :visible="previewVisible" :footer="null" @cancel="previewVisible=false" width="60%">
      <img alt="example" style="width: 100%" :src="previewImage" />
    </a-modal>
  </a-drawer>
</template>

<script>
import { uploadFile,deleteFile } from '@/api/common'
import { getFileName } from '@/utils/util'
import { showMessage } from '@/utils/mixins'
import { updateProductTrait,addProductTrait } from '@/api/manage'

export default {
  mixins: [showMessage],
  props: {
    parent: {
      type: Object,
      default: () => ({})
    }
  },
  data() {
    return {
      tabkey: '1',
      visible: false,
      labelCol: {span: 4},
      wrapperCol: {span: 20},
      treeData: [],
      form: {
        menuId: undefined,
        title: undefined,
        titleEn: undefined,
        bgImage: undefined,
        description: undefined,
        descriptionEn: undefined,
        orderNum: undefined
      },
      rules: {
        menuId:[{required: true,message: '菜单所属不能为空',trigger: 'change'}],
        title: [{required: true,message: '菜单名称不能为空',trigger: 'blur'}],
        titleEn: [{required: true,message: '英文菜单名称不能为空',trigger: 'blur'}],
        description: [{required: true,message: '特点描述不能为空',trigger: 'blur'}],
        descriptionEn: [{required: true,message: '英文特点描述不能为空',trigger: 'blur'}],
      },
      status: undefined,
      fileList: [],
      previewVisible: false,
      previewImage: undefined,
      id: undefined,
    }
  },
  methods: {
    // 删除图片
    hanldeImgRemove(file) {
      deleteFile({filePath: file.url}).then(res => {
        if(res.code == '0') {
          this.showMessage(res,() => {
            this.fileList = []
          })
        }
      })
    },
    //自定义上传图片
    handleUploadChange(file) {
      file = file.file
      const _this = this
      const formdata = new FormData()
      formdata.append('file', file)
      uploadFile(formdata).then(res => {
        if(res.code == '0') {
          file.url = process.env.VUE_APP_API_ORIGIN+res.data
          file.uid = Math.random()
          file.fileame = getFileName( res.data )
          _this.fileList.push(file)
          _this.form.bgImage = res.data
        }else {
          _this.$message.error(res.message)
        }
      })
    },
    // 预览
    async handlePreview(file) {
      if (!file.url && !file.preview ) {
        file.preview = await getBase64(file.originFileObj)
      }
      this.previewImage = file.url || file.preview
      this.previewVisible = true
    },
    // 提交
    onSubmit () {
      this.$refs.ruleForm.validate(vaild => {
        if(vaild) {
          if(!this.form.bgImage) {
            this.$message.warning('特点图片不能为空！')
            return
          }
          if(this.status==1) {
            addProductTrait(this.form).then(res => {
              if(res.code == '0') {
                this.showMessage(res,() => {
                  this.parent.getList()
                  this.onClose()
                })
              }
            })
          }else {
            updateProductTrait({
              id: this.id,
              ...this.form
            }).then(res => {
              this.showMessage(res,() => {
                this.parent.getList()
                this.onClose()
              })
            })
          }
        }else {
          return false
        }
      })
    },
    // 关闭
    onClose() {
      this.status = undefined
      this.visible = false
      this.id = undefined
      for(const key in this.form) {
        this.form[key] = undefined
      }
      this.tabkey = '1'
      this.fileList = []
    }
  }
}
</script>